package com.eqdd.yiqidian.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import com.eqdd.yiqidian.base.BaseApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;


public class SPUtil {

    private static final String ACCOUNT = "account";
    private static final String PASSWORD = "password";
    private static final String FIRST = "first";
    private static final String UUID = "uuid";
    private static final String TOKEN = "token";
    private static final String ID = "id";
    private static final String JPUSH = "jpush";
    private static final String NO_CLEAR = "noClear";

    public static void saveAdLink(String url) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(NO_CLEAR, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("adLink", url);
        edit.apply();
    }

    public static String getAdLink() {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(NO_CLEAR, Context.MODE_PRIVATE);
        return preferences.getString("adLink", "");
    }

    public static void saveAd(String url) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(NO_CLEAR, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("ad", url);
        edit.apply();
    }

    public static String getAd() {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(NO_CLEAR, Context.MODE_PRIVATE);
        return preferences.getString("ad", "");
    }

    public static void saveJPushAlias(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(JPUSH, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(key, true);
        edit.apply();
    }

    public static boolean isSaveJPushAlias(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(JPUSH, Context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
    }

    public static void savetID(String key, Long id) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(ID, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.clear();
        edit.putLong(key, id);
        edit.apply();
    }

    public static Long getID(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(ID, Context.MODE_PRIVATE);
        return preferences.getLong(key, -1);
    }


    public static void saveToken(String key, String uuid) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(TOKEN, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, uuid);
        edit.commit();
    }

    public static String getToken(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(TOKEN, Context.MODE_PRIVATE);
        return preferences.getString(key, "");
    }

    public static void saveUUID(String key, String uuid) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(UUID, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, uuid);
        edit.commit();
    }

    public static String getUUID(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(UUID, Context.MODE_PRIVATE);
        return preferences.getString(key, "");
    }

    public static void saveFirst(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(FIRST, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(key, false);
        edit.apply();
    }

    public static boolean getFirst(String key) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(FIRST, Context.MODE_PRIVATE);
        return preferences.getBoolean(key, true);
    }

    public static void saveAccount(String key, String account) {
        clearAccount();
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, account);
        edit.apply();
    }

    public static String getPassword(String key, String defalut) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(PASSWORD, Context.MODE_PRIVATE);
        return preferences.getString(key, defalut);
    }

    public static void savePassword(String key, String password) {
        clearPassword();
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(PASSWORD, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, password);
        edit.apply();
    }

    public static String getAccount(String key, String defalut) {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE);
        return preferences.getString(key, defalut);
    }


    public static void clearAccount() {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.clear().apply();
    }

    public static void clearPassword() {
        SharedPreferences preferences = BaseApplication.getApplication().getSharedPreferences(PASSWORD, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.clear().apply();
    }

    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "share_date";


    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param
     * @param key
     * @param object
     */
    public static void setParam(String key, Object object) {


        SharedPreferences sp = BaseApplication.getApplication().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if (object == null || (object.getClass().getSimpleName().equals("String")) && ((String) object).length() == 0) {
            editor.putString(key, "");
            editor.apply();
            return;
        }

        //创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //创建字节对象输出流
        ObjectOutputStream out = null;
        try {
            //然后通过将字对象进行64转码，写入key值为key的sp中
            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            editor.putString(key, objectVal);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        editor.commit();
    }


    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object getParam(String key, Object defaultObject) {
        SharedPreferences sp = BaseApplication.getApplication().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, "");
            if (TextUtils.isEmpty(objectVal)) {
                return defaultObject;
            }
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            //一样通过读取字节流，创建字节流输入流，写入对象并作强制转换
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return defaultObject;
    }

    public static Object getParam(String key) {
        SharedPreferences sp = BaseApplication.getApplication().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, "");
            if (TextUtils.isEmpty(objectVal)) {
                return null;
            }
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            //一样通过读取字节流，创建字节流输入流，写入对象并作强制转换
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    public static void clearData() {
        SharedPreferences sp = BaseApplication.getApplication().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear().apply();
    }
}

